<?php

namespace Admin\Controller;
use Think\Controller;
//加载GatewayClient
require_once SITE_PATH.'/vendor/workerman/GatewayClient/Gateway.php';
use GatewayClient\Gateway;

class IntegralController extends BaseController{
	protected function message_record($user_id,$message,$send_user_id = 1){
		if($user_id == 0){
			$user = M('User') -> select();
			foreach($user as $key => $val){
				$data['user_id'] = $val['id'];
				$data['create_time'] = time();
				$data['message'] = $message;
				$data['send_user_id'] = $send_user_id;
				$messageRecord = M('message');
				$message_res = $messageRecord ->add($data);		
			}
		}else{
			$data['user_id'] = $user_id;
			$data['create_time'] = time();
			$data['message'] = $message;
			$data['send_user_id'] = $send_user_id;
			$messageRecord = M('message');
			$message_res = $messageRecord ->add($data);			
		}

		if($message_res){
			return true;
		}else{
			return false;
		}
	}
	//资金记录
	protected function money_record($user_id,$amount,$status){
		$data['create_time'] = time();
		$data['uid'] = $user_id;
		$data['amount'] = $amount;
		$data['status'] = $status;
		$MoneyRecord = M('money_record');
		$money_record_res = $MoneyRecord ->add($data);
		if($money_record_res){
			return true;
		}else{
			return false;
		}
	}

	protected function send_message_touid($uid,$message){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToUid($uid, $message);
	}
	protected function send_message_toall($message,$client_id_array=null ,$exclude_client_id){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToAll($message,$client_id_array=null ,Gateway::getClientIdByUid($exclude_client_id));
	}
	
	public function bind(){
		$client_id = I('client_id');
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 假设用户已经登录，用户uid和群组id在session中
		$username      = $_SESSION['admin']['username'];
		$group_id = 2;
		// client_id与uid绑定
		Gateway::bindUid($client_id, $username);
		// 加入某个群组（可调用多次加入多个群组）
		Gateway::joinGroup($client_id, $group_id);
	}

	//投诉管理
	public function complain(){
		$complain = M('complain');
		$trade_hall_id = I('trade_hall_id');
		$start = I('starttime');
		$end = I('endtime');
		if($trade_hall_id){
			$map['trade_hall_id'] = $trade_hall_id;
		}
		if($strat){
			$map['time'] = array('egt',strtotime($start));
		}
		if($end){
			$map['time'] = array('elt',strtotime($end));
		}
		if($start && $end){
			$map['time'] = array(array('egt',strtotime($start)),array('elt',strtotime($end)),'AND');
		}
		
		$count = $complain->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $complain->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
		
		for($i=0;$i<count($list);$i++){
			$list[$i]['user'] = M('user')->where("id = {$list[$i]['user_id']}")->find();
			$list[$i]['trade'] = M('trade_hall')->where("id = {$list[$i]['trade_hall_id']}")->find();
		}
		$this->assign('trade_hall_id',$trade_hall_id);
		$this->assign('starttime',$start);
		$this->assign('endtime',$end);
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display();
	}
   	//建议
   	public function feedback(){
		$feedback = M('feedback');
		if($trade_hall_id){
			$map['trade_hall_id'] = $trade_hall_id;
		}
		if($strat){
			$map['time'] = array('egt',strtotime($start));
		}
		if($end){
			$map['time'] = array('elt',strtotime($end));
		}
		if($start && $end){
			$map['time'] = array(array('egt',strtotime($start)),array('elt',strtotime($end)),'AND');
		}
		
		$count = $feedback->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $feedback->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
		
		for($i=0;$i<count($list);$i++){
			$list[$i]['user'] = M('user')->where("id = {$list[$i]['user_id']}")->find();
		}
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display();
   	}
	public function complaindel(){
		$id = I('id');
		M('complain')->where("id = $id")->delete();
		$this->success("删除成功");
	}
	
	
	public function complainendisable(){
		$id = I('id');
		$res = M('complain')->where("id = $id")->setField('status',2);
		$info = M('complain')->where("id = $id")->find();
		if($res){
			$this->message_record($info['user_id'],'您的投诉已经处理，请注意查看矿石到账，如有疑问请联系客服！');
			$this->success('处理成功！');
		}else{
			$this->error('处理失败！');
		}
	}
	public function feedbackdel(){
		$id = I('id');
		M('feedback')->where("id = $id")->delete();
		$this->success("删除成功");
	}
	
	
	public function feedbackendisable(){
		$id = I('id');
		$res = M('feedback')->where("id = $id")->setField('status',2);
		$info = M('feedback')->where("id = $id")->find();
		if($res){
			$this->message_record($info['user_id'],'您的建议已查看，如有疑问请联系客服！');
			$this->success('处理成功！');
		}else{
			$this->error('处理失败！');
		}
	}
	public function lists(){
		$integral = M('integral');
		$nickname = I('nickname');
		$start = I('starttime');
		$end = I('endtime');
		if($nickname){
			$map['nickname'] = array('like','%'.$nickname.'%');
		}
		if($strat){
			$map['time'] = array('egt',strtotime($start));
		}
		if($end){
			$map['time'] = array('elt',strtotime($end));
		}
		if($start && $end){
			$map['time'] = array(array('egt',strtotime($start)),array('elt',strtotime($end)),'AND');
		}
		
		$count = $integral->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $integral->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
		
		for($i=0;$i<count($list);$i++){
			$list[$i]['user'] = M('user')->where("id = {$list[$i]['uid']}")->find();
		}
		
		$this->assign('nickname',$nickname);
		$this->assign('starttime',$start);
		$this->assign('endtime',$end);
		$this->assign('nickname',$nickname);
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display('lists');
	}
	public function prop_edit(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$data = $_POST;
				$data['prop_exp_time'] = $data['prop_exp_time'] * 24 * 60 * 60;
				$res2 = M('prop')->save($data);
				if($res2){
					$this->success('修改成功,跳转中~',U('/Admin/integral/prop'),1);
				}else{
					$this->error('修改失败！');
				}
			}
		}else{
			$id = I('id');
			$propinfo = M('prop')->where("id = $id")->find();
			$propinfo['prop_exp_time'] = $propinfo['prop_exp_time']/24/60/60;
			$this->assign('propinfo',$propinfo);
			$this->display();
		}
	}
	
	
	public function gonggao(){

		if(IS_POST){
			$gongao = M('message');
			$date=$_POST;
			$user_ids=M('user')->where(['status'=>1])->getField("id",true);

			if (!empty($user_ids)){
			    foreach ($user_ids as $val){
			        $message_add=[
			            'uid'=>$val,
			            'send_uid'=>0,
			            'title'=>"管理员通知",
			            'content'=>$date['gonggao'],
			            'read_time'=>'',
			            'is_read'=>0,
			            'type'=>1,
			            'create_time'=>time(),
			            'warning_type'=>'',
                    ];

			        $success=$gongao->add($message_add);
                }
            }
            if ($success){
                $this->success("发送成功！");
            }else{
			    $this->error("发送失败！");
            }

		}else{
			$this->display();
		}
	}
	public function prop(){
		$prop_name = I('prop_name');
        if($prop_name){
            $member = M('prop');
            $count = $member->where("prop_name like '%{$prop_name}%'")->count();
            $page = new \Think\Page($count,10);
            $show = $page->show();
            $list = $member->where("prop_name like '%{$prop_name}%'")->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
        }else{
            $member = M('prop');
            $count = $member->count();
            $page = new \Think\Page($count,10);
            $show = $page->show();
            $list = $member->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
        }
		
		foreach($list as $key => $value){
            $list[$key]['prop_exp_time'] = $value['prop_exp_time']/24/60/60;
           
            if($list[$key]['prop_exp_time'] >= 24855){
            	$list[$key]['prop_exp_time'] = '无限期';
            }else{
            	$list[$key]['prop_exp_time'] = $list[$key]['prop_exp_time'].'天';
            }
        }
        
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	public function index(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确');
			}else{
				$userid = I('userid');
				$points = I('points');
				//处理充值天数
                $deadlinedata=$points*3600*24;


				
				$res2 = M('user')->where("id = $userid")->setInc('deadline',$deadlinedata);
				if($res2){
					$info = M('user')->where("id = $userid")->find();
					$message_points = array(
						'uid'=>$userid,
						'send_uid'=>0,
						'type' => 1,
						'is_read' => 0,
						'read_time' => '',
						'warning_type' => '',
						'title' =>"充值消息",
						'content'=>	"管理员已帮您充值".$points."天使用时间，请查看账户余额情况！",
						'create_time'=>time()
					);
					M('message')->add($message_points);

//					$this->send_message_touid($userid,json_encode($message_points));
//					$this->message_record($userid,'管理员有帮您操作充值，请查看余额情况！');
//					$type = 2;
//					$code = 5;
//					$status = 1;
					$this->money_record($userid,$points,1);
							
					$this->success('充值成功,跳转中~',U('Admin/Member/index'),1);
				}else{
					$this->error('充值失败！');
				}
			}
		}else{
			$id = I('id');
			$userinfo = M('user')->where("id = $id")->find();
			$this->assign('userinfo',$userinfo);
			$this->display();
		}
		
	}
	
	public function under(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$userid = I('userid');
				$points = I('points');
				$info = M('user')->where("id = $userid")->find();
				if($info['balance']<$points){
					$this->error('矿石数量不足');
				}
				
				$res2 = M('user')->where("id = $userid")->setDec('balance',$points);
				if($res2){
					$message_points = array(
						'from'=>'admin',
						'type' => 'points',
						'content'=>	'管理员有帮您操作矿石提现，请查看提现账户余额情况！',
						'time'=>date('H:i:s')
					);
					$this->send_message_touid($userid,json_encode($message_points));
					$this->message_record($userid,'管理员有帮您操作矿石提现，请查看提现账户余额情况！');
					$type = 1;
					$code = 6;
					$status = 1;
					$this->money_record($userid,$type,$points,$code,$status);
					$this->success('提现成功,跳转中~',U('Admin/Member/index'),1);
				}else{
					$this->error('提现失败！');
				}
			}
		}else{
			$id = I('id');
			$userinfo = M('user')->where("id = $id")->find();
			$this->assign('userinfo',$userinfo);
			$this->display();
		}
	}
	
}


?>